#include "GameScene.h"
#include "SimpleAudioEngine.h"   
#include "AudioManager.h"
using namespace CocosDenshion;

GameLayer *s_GameLayer = NULL;
GSpriteLayer *spritelayer;
GScoreLayer *scoreLayer;

GameLayer::~GameLayer()
{
	spritelayer = NULL;
	scoreLayer = NULL;
	s_GameLayer = NULL;
	SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}
//void GameLayer::onExit()
//{
//	s_GameLayer = NULL;
//	SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
//}

CCScene* GameLayer::scene(int id)
{
	CCScene * scene = NULL;
	do 
	{
		// 'scene' is an autorelease object
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		// 'layer' is an autorelease object
		s_GameLayer = GameLayer::create();
		GPauseLayer *pauselayer = GPauseLayer::create();

		GItemLayer *itemlayer = GItemLayer::create();
		itemlayer->setTouchEnabled(true);
		spritelayer = GSpriteLayer::create();

		scoreLayer =GScoreLayer::create();

		scene->addChild(pauselayer,6);
		scene->addChild(itemlayer,7);
		scene->addChild(spritelayer,5);
		scene->addChild(scoreLayer,8);
		scene->addChild(s_GameLayer,1,1);

		SimpleAudioEngine::sharedEngine()->preloadEffect("PetSound00.mp3");
		SimpleAudioEngine::sharedEngine()->preloadEffect("BuyItem.mp3");
		SimpleAudioEngine::sharedEngine()->preloadEffect("GiftBox01.mp3");
		SimpleAudioEngine::sharedEngine()->preloadEffect("GiftBox02.mp3");
		SimpleAudioEngine::sharedEngine()->preloadEffect("GiftBox03.mp3");
	} while (0);

	// return the scene
	return scene;
}
GameLayer* GameLayer::sharedGameLayer()
{
	return s_GameLayer;
}

void GameLayer::restart()
{
	spritelayer->resetFoodSprite();
	scoreLayer->score =0;
}

void GameLayer::changeSpeed(bool isdoubleSpeed)
{
	spritelayer->changeSpeed(isdoubleSpeed);
}
void GameLayer::addScore(int score)
{
	int s = scoreLayer->score;
	scoreLayer->score = s +score;
}

float maxTime;

bool GameLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());
		CCSize size =CCDirector::sharedDirector()->getWinSize();

		CCSprite *background =CCSprite::create("gamebg.png");
		background->setPosition(ccp(size.width/2, size.height/2));
		this->addChild(background, -9999);

		//CCSprite *left =CCSprite::create("jungle-left.png");
		//left->setAnchorPoint(ccp(0, 0));
		//left->setPosition(ccp(0, 0));
		//this->addChild(left, -9998);
		//CCSprite *right =CCSprite::create("jungle-right.png");
		//right->setAnchorPoint(ccp(1, 0));
		//right->setPosition(ccp(size.width, 0));
		//this->addChild(right, -9998);


		player =FoodPlayer::create();
		player->setAnchorPoint(ccp(0.5f,0));
		player->setPosition(ccp(size.width/2, 0));
		this->addChild(player);

		CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("Items01.plist");
		this->setTouchMode(kCCTouchesOneByOne);
		CCArray *animFrames = CCArray::create();
		char str[100] = {0};
		for(int i = 1; i < 18; i++) 
		{

			sprintf(str, "hlb%d.png",i);
			CCSpriteFrame* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);
			animFrames->addObject(frame);
		}
		CCSprite* sprite1 = CCSprite::createWithSpriteFrameName("hlb1.png");
		sprite1->setPosition(ccp(size.width/2 , size.height/2 + sprite1->getContentSize().height /3));

		CCAnimation* animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);
		sprite1->runAction(CCRepeatForever::create( CCAnimate::create(animation) ) );

		this->addChild(sprite1, 0);



		//----
		CCSprite *timeSprite =CCSprite::create("orange_edit.png");
		timeSprite->setPosition(ccp(0,size.height - 50));
		timeSprite->setScaleX(1);
		timeSprite->setAnchorPoint(ccp(0,0.5f));
		this->addChild(timeSprite,1,2);
		maxTime = 30;


		SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_GAME, true);

		bRet = true;
	} while (0);

	return bRet;
}

float m_time;

void  GameLayer::updateState(PlayerState state)
{
	player->setState( state);
}

